﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.Web;
using System.Security.Principal;
using System.Collections;
using System.DirectoryServices;

namespace CMD.BAL.AUTH
{
    [DataContractAttribute]
    public class AuthUserRol
    {
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string user;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public  List<Rol> aIdRol;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string idRol;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string dscRol;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string mail;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public Boolean admin;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public Boolean reporte;

        public static string AuthUser() 
        {
            //HttpContext.Current.User.Identity.Name(ISAAC-PC\ISAAC)
            // System.Environment.UserName;(ISAAC)
            //System.Environment.UserDomainName(ISAAC-PC)
            //string user = System.Environment.UserName;
            string user = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.Split('\\')[1].ToString();

            return user;
        }

        public static List<Rol> GetDataListRol(string[] idRol,ref Boolean admin,ref Boolean reporte)
        {
            string join;
            if (idRol == null)
            {
                join = "";
            }
            else
            {
                join = String.Join("'',''", idRol);
                join = "'''" + join + "'''";
            }
            try
            {
                DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GetUserRolByGroup " + join + "");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    DataRow rowABorrar;
                    int length = dt.Rows.Count;
                    for (int i = 0; i < length; i++)
                    {
                        if (dt.Rows[i]["UNID_ROL"].ToString() == System.Configuration.ConfigurationManager.AppSettings["admin"].ToString() ||
                            dt.Rows[i]["UNID_ROL"].ToString() == System.Configuration.ConfigurationManager.AppSettings["reportes"].ToString())
                        {
                            if(!admin)
                                admin = (dt.Rows[i]["UNID_ROL"].ToString() == System.Configuration.ConfigurationManager.AppSettings["admin"].ToString()) ? true : false;
                            if(!reporte)
                                reporte = (dt.Rows[i]["UNID_ROL"].ToString() == System.Configuration.ConfigurationManager.AppSettings["reportes"].ToString()) ? true : false;
                            rowABorrar = dt.Rows[i];
                            dt.Rows.Remove(rowABorrar);
                            dt.AcceptChanges();
                            length = dt.Rows.Count;
                            i--;
                        }
                    }
                    //foreach (DataRow row in dt.Rows)
                    //{
                    //    if (row[""].ToString() == System.Configuration.ConfigurationManager.AppSettings["RutaDestino"].ToString() || row[""].ToString() == System.Configuration.ConfigurationManager.AppSettings["RutaDestino"].ToString())
                    //    {
                    //        rowABorrar = row;

                    //    }
                    //}
                    List<Rol> list = dt.AsEnumerable().Select(
                        row => new CMD.BAL.AUTH.Rol
                        {
                            idRol = String.IsNullOrEmpty(row.Field<Int64>(0).ToString())
                            ? ""
                            : row.Field<Int64>(0).ToString(),
                            rol = String.IsNullOrEmpty(row.Field<string>(1))
                            ? ""
                            : row.Field<string>(1),
                            prio= String.IsNullOrEmpty(row.Field<Int32>(2).ToString())
                            ? 0
                            : row.Field<Int32>(2),
                            dscRol = String.IsNullOrEmpty(row.Field<string>(1))
                            ? ""
                            : row.Field<string>(1),
                        }
                        ).ToList();

                    return list;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static Rol GetRol(string idRol)
        {
            try
            {
                Rol r;
                DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GETROL'" + idRol + "'"); 
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (idRol == "")
                {
                    return null;
                }
                else
                {
                    r = new Rol (Convert.ToString(dt.Rows[0]["UNID_ROL"]),0,Convert.ToString(dt.Rows[0]["DSC_ROL"]));
                }
                return r;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static string GetMailUser(string userName)
        {
            System.Security.Principal.WindowsIdentity usuario = System.Security.Principal.WindowsIdentity.GetCurrent();
            DirectoryEntry de = new DirectoryEntry();
            DirectorySearcher ds = new DirectorySearcher(de, "(objectClass=user)");
            ds.Filter = "(sAMAccountName=" + userName + ")";
            SearchResult result1 = ds.FindOne();
            ResultPropertyValueCollection value = result1.Properties["mail"];
            return value[0].ToString();
            //return "gmail00000000000000000000000000@gmail.com";
        }


        public AuthUserRol() 
        {
        }

        public AuthUserRol(string user,List<Rol> aIdRol,string idRol, string dscRol,string mail,Boolean admin, Boolean reporte)
        {
            this.user = user;
            this.aIdRol = aIdRol;
            this.idRol = idRol;
            this.dscRol = dscRol;
            this.mail = mail;
            this.admin = admin;
            this.reporte = reporte;
        }

    }
}
